<template>
  <div>
    <my-header
      ><el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item></my-header
    >
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7"
          ><!-- 输入框 -->
          <el-input placeholder="请输入账户名" clearable v-model="inputValue" @clear="getUserList">
            <template #append>
              <el-button icon="Search" type="primary" @click="onSearchlist"></el-button>
            </template> </el-input
        ></el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="userlist" style="width: 100%" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="账户名"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="200px"></el-table-column>
        <el-table-column prop="phone" label="电话"></el-table-column>
        <el-table-column prop="name" label="昵称"></el-table-column>
        <el-table-column label="性别">
          <template #default="scope">
            <el-tag class="ml-2" type="danger" v-if="scope.row.sex === '0'">女</el-tag>
            <el-tag class="ml-2" type="primary" v-else>男</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="point" label="积分" sortable></el-table-column>
        <el-table-column prop="createTime" label="创建时间" sortable></el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              inline-prompt
              active-text="已启用"
              inactive-text="已停用"
              @change="userStateChanged(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="Delete"
              size="small"
              @click="removeUserById(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[1, 2, 5, 10]"
        small="small"
        background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="current"
        :page-size="size"
        :total="this.userlist.length"
      />
    </el-card>
  </div>
</template>

<script>
import axios from 'axios'
import { getUser, updateUser } from '../../api/user.js'
export default {
  data() {
    return {
      inputValue: '',
      userlist: []
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    onSearchlist() {
      this.userlist = this.userlist.filter((item) => item.username.includes(this.inputValue))
    },
    // 分页
    handleSizeChange(val) {
      this.size = val
      this.getUserList()
    },
    // 分页
    handleCurrentChange(val) {
      this.current = val
      this.getUserList()
    },
    // 获取用户列表
    async getUserList() {
      const { data: res } = await getUser()
      if (res.code !== 1) {
        return this.$message.error('获取用户列表失败')
      }
      this.userlist = res.data
    },
    // 删除用户
    async removeUserById(userInfo) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm('此操作将永久删除该用户，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => {
        return err
      })
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = axios.delete('/content/user/delete', { data: { id: userInfo.id } })
      if (res.code !== 1) {
        return this.$message.error('删除用户失败！')
      }
      this.getUserList()
      this.$message.success('删除成功！')
    },
    // 编辑用户状态
    async userStateChanged(userInfo) {
      const { data: res } = await updateUser({ id: userInfo.id })
      if (res.code !== 1) {
        return this.$message.error('修改失败！')
      } else {
        this.getUserList()
        return this.$message.success('修改成功！')
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
